import { defineConfig } from 'vite';
import { resolve } from 'node:path';
import legacy from '@vitejs/plugin-legacy';
import visualizer from 'rollup-plugin-visualizer';

const isProd = process.env.NODE_ENV === 'production';

export default defineConfig({
  server: {
    host: '0.0.0.0',
    port: 7503,
    proxy: {
      '/admin-api': {
        target: 'http://localhost:7501',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/admin-api/, '/api'),
      },
      '/ram-api': {
        target: 'http://localhost:7504/',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/ram-api/, '/api'),
      },
    },
  },
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src'),
    }
  },
  build: {
    outDir: 'dist',
    minify: isProd,
    sourcemap: isProd ? false : 'inline',
    target: isProd ? 'es2015' : undefined,
    rollupOptions: {
      output: {
        // format: isProd ? 'cjs' : undefined,
        chunkFileNames: 'assets/js/[name]-[hash].js', // 代码块文件名
        entryFileNames: 'assets/js/[name]-[hash].js', // 入口文件名
        assetFileNames: 'assets/[ext]/[name]-[hash].[ext]', // 资源文件名
        // manualChunks: isProd
        //   ? {
        //     React: ['react'],
        //     ReactDOM: ['react-dom'],
        //     dayjs: ['dayjs'],
        //     alova: ['alova'],
        //     '@douyinfe/semi-icons': ['@douyinfe/semi-icons'],
        //     '@douyinfe/semi-ui': ['@douyinfe/semi-ui'],
        //   }
        //   : undefined,
        // minifyInternalExports: isProd,
      },
    },
  },
  /* 可以加上这个配置，把静态资源放到 CDN 上面 */
  // base: isProd ? '//cdn.yixiantong.com/' : '/',
  base: '/admin',
  plugins: [
    false &&
      visualizer({ open: true, gzipSize: true, brotliSize: true }),
    // legacy({
    //   targets: ['defaults', 'not IE 11'] // 针对不是IE11的默认浏览器
    // }),
  ],
});
